package com.findyou.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * @Title: JDBC
 * @Author 贾金博
 * @Package com.findyou.config
 * @Date 2024/12/6 上午9:06
 * @description: 数据库配置
 */
@Component
@PropertySource("classpath:JDBC.properties")  // 加载外部配置文件 JDBC.properties
public class JDBCConfig {

    @Value("${jdbc.driverClassName}")
    String driverClassName;

    @Value("${jdbc.url}")
    String url;

    @Value("${jdbc.username}")
    String username;

    @Value("${jdbc.password}")
    String password;


    @Bean // 放到容器里面
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource; // 返回的值被加入到Spring容器里面
    }

}
